//
//  TZplayController.m
//  XMLYFM
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 zzl. All rights reserved.
//


//我随便写的不用在意  忽略不计

#import "TZplayController.h"
//引入框架
#import <AVFoundation/AVFoundation.h>

#import <STKAudioPlayer.h>

#import "AppTools.h"

#import <UIImageView+WebCache.h>

#import "TZEditorDetailModel.h"


@interface TZplayController ()

@property(nonatomic,strong)STKAudioPlayer *player;
@property(nonatomic,retain)NSTimer *myTimer;

//毛玻璃
@property(nonatomic,strong)UIVisualEffectView *blurView;

@end

@implementation TZplayController


//将player页设成单例
+(instancetype)sharaMusicPlayer
{
    static TZplayController *playerVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        playerVC = [[TZplayController alloc] init];
        
    });
    
    return playerVC;
}



- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title  = @"音乐";
    [self creatAVplayer];
    
    
    
}
-(void)setMusicId:(NSInteger)musicId
{
    [self getDataID:musicId];


}

-(void)creatAVplayer
{
  
    //播放对象
    self.player = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
   
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    
    self.blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
    
    self.blurView.alpha = 0.7;
    
    self.blurView.frame = self.maoBoLi.bounds;
    [self.maoBoLi addSubview:self.blurView];

    
    //定时器
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    
    
}


-(void)getDataID:(NSInteger)myId
{
 
    
    NSString *Str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId= %ld",myId];
    
    
    [AppTools getDataWithURL:Str Block:^(id result) {
    NSMutableDictionary *Dic = result;
    NSString *Str = Dic[@"playUrl64"];
    NSLog(@"%ld🐴🐴",myId);
    [self.player play:Str];
        
        
        [self.maoBoLi sd_setImageWithURL:[NSURL URLWithString:[Dic objectForKey:@"coverLarge"]] placeholderImage:PLACEHOLDER_IMAGE];
        [self.LittleImage sd_setImageWithURL:[NSURL URLWithString:[Dic objectForKey:@"coverLarge"]] placeholderImage:PLACEHOLDER_IMAGE];
        self.navigationItem.title = Dic[@"albumTitle"];
        
} ErrorBlock:^(NSError *error) {
    
    
    
}];



    
}



//每一秒更新一次
-(void)timeAction
{
    //设置进度条的最大值为总时长
    self.playerSilder.maximumValue = self.player.duration;
    self.playerSilder.value =  self.player.progress;//当前播放时长
    
    //总时长
    NSInteger totalMin = self.player.duration/60;
    NSInteger totalSec = (NSInteger)self.player.duration%60;
    // 当前时间
    NSInteger currentMin = (NSInteger)self.player.progress/60;
    NSInteger currentSec = (NSInteger)self.player.progress % 60;
    
    
//    self.myLabel.text = [NSString stringWithFormat:@"%02ld:%02ld/%02ld:%02ld",currentMin,currentSec,totalMin,totalSec];
    
    //获取播放状态
    if (self.player.state ==  STKAudioPlayerStateStopped) {
        
//      NSLog(@"播放停止");
    }
    
    
    [self.playerSilder addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];

}


//播放
- (IBAction)bofang:(id)sender {
    if (self.player.state == STKAudioPlayerStatePaused) {
        [self.bofang setTitle:@"播放" forState:UIControlStateNormal];
        [self.player resume];
    }
    else if (self.player.state == STKAudioPlayerStatePlaying)
    {
        [self.bofang setTitle:@"暂停" forState:UIControlStateNormal];
        [self.player pause];
    
    }
}

//下一曲
- (IBAction)nextAction:(UIButton *)sender {
    //+1后对数组取余 是为了防止多次点击,数组越界
    
    self.indexRow = (self.indexRow + 1)%self.SourceArray.count;
    
//    NSLog(@"🐷🐷🐷🐷%ld",self.indexRow);
        TZEditorDetailModel *editor = self.SourceArray[self.indexRow];
        self.musicId = editor.trackId;
   
   
   

}

////上一曲
- (IBAction)shangAction:(UIButton *)sender {
    
//     NSLog(@"🐷🐷🐷🐷%ld",self.indexRow);
    
        if (self.indexRow == 0) {
            self.indexRow = (self.SourceArray.count - 1);
        }
    else
    {
    self.indexRow = (self.indexRow - 1);
    
    }
    NSLog(@"🐷🐷🐷🐷%ld",self.indexRow);
    TZEditorDetailModel *editor = self.SourceArray[self.indexRow];
    self.musicId = editor.trackId;
    
    
}




-(void)sliderAction
{

//改变播放进度
    [self.player seekToTime:self.playerSilder.value];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}









/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/








@end














